package IndexComponent;

import java.nio.ByteBuffer;

import PagedFile.Buffer;

public class BPTree_FileHead {
	/*
	 * A B+ tree file head contains meta data as follows:
	 * length(B)		contents
	 * 	4				Order
	 * 	4				Level
	 * 	4				Height
	 * 	4				KeywordType
	 * 	4				KeywordLength
	 * 	4				RootPageNum
	 * 	4				FirstLeafPageNum
	 * sum is 28 Bytes;
	 */
	public static final int IX_FILEHEAD_SIZE = 28;
	int Order;
	int Level;
	int Height;
	int KeywordType;
	int KeywordLength;
	int RootPageNum;
	int FirstLeafPageNum;
	
	public BPTree_FileHead(int attrType, int attrLength) {
		if ( attrType == Keyword.KEYTYPEINT && attrLength != 4 )
			throw new IX_Abort_Exception("Fatal");
		KeywordType = attrType;
		KeywordLength = attrLength;
		Order = (Buffer.BLOCK_SIZE - BPlusTreeNode.PAGEHEADSIZE - 4 ) / (KeywordLength+4);
		Level = (Buffer.BLOCK_SIZE - BPlusTreeNode.PAGEHEADSIZE ) / (KeywordLength+BPlusTreeNode.DataLength);
		Height = 1;
		RootPageNum = 0;
		FirstLeafPageNum = 0;
	}
	public BPTree_FileHead(ByteBuffer bb) {
		get_from_ByteBuffer(bb);
	}
	
	public String toString(){
		String ret = "(";
		ret += this.Order + ", ";
		ret += this.Level + ", ";
		ret += this.Height + ", ";
		ret += this.KeywordType + ", ";
		ret += this.KeywordLength + ", ";
		ret += this.RootPageNum + ", ";
		ret += this.FirstLeafPageNum;
		ret += ")";
		return ret;
	}

	public void put_into_ByteBuffer(ByteBuffer bb) {
		bb.position(0);
		bb.putInt(this.Order);
		bb.putInt(this.Level);
		bb.putInt(this.Height);
		bb.putInt(this.KeywordType);
		bb.putInt(this.KeywordLength);
		bb.putInt(this.RootPageNum);
		bb.putInt(this.FirstLeafPageNum);
	}
	
	public void get_from_ByteBuffer(ByteBuffer bb) {
		bb.position(0);
		this.Order = bb.getInt();
		this.Level = bb.getInt();
		this.Height = bb.getInt();
		this.KeywordType = bb.getInt();
		this.KeywordLength = bb.getInt();
		this.RootPageNum = bb.getInt();
		this.FirstLeafPageNum = bb.getInt();
	}

}
